#pragma once
#ifndef AbstractProcedure_h__
#define AbstractProcedure_h__

#include <QObject>
#include <QSslSocket>
#include <QNetworkReply>

#include "tccore_global.h"

namespace TC
{
    namespace Request
    {
        class TCCORE_EXPORT AbstractProcedure : public QObject
        {
            Q_OBJECT
                Q_PROPERTY(Status status READ status NOTIFY statusChanged)
                Q_PROPERTY(QString errorString READ errorString)
                Q_PROPERTY(QString failedString READ failedString NOTIFY failedStringChanged) 

        public:
            enum class Status {
                Ready = 0,
                Communicating = 1,
                Ok = 2,
                Failed = 3,
                Error = 4
            };
			Q_ENUM(Status);				
                
        public:
            AbstractProcedure(QObject *parent = nullptr);
            virtual ~AbstractProcedure();
        public:
            Status status();
            QString errorString();
            QString failedString() { return _failedString; }
        signals:
            void ready(); // only emited when status is *back* to Status::Ready.
            void communicationing();
            void ok();
            void failed();
            void error(const QString &reason);
            void statusChanged();
            void failedStringChanged();

        protected:
            void setReady();
            void setCommunicationing();
            void setOk();
            void setFailed();
            void setError(const QString &reason);
            void setFailedString(QString newVal) { if (_failedString != newVal) { _failedString = newVal; failedStringChanged(); } }

        protected:
            void autoSetHttpError(QNetworkReply::NetworkError code);
            void autoSetSocketError(QAbstractSocket::SocketError socketError);
			void autoSetSslError(const QList<QSslError> &errors);
		private:
            Status _status = Status::Ready;
            QString _errorString;
            QString _failedString;

        };
    }
}
#endif // AbstractProcedure_h__
